import { useState } from "react";
import { RouterRefreshContext } from "./refresh-context";

/**
 * 路由刷新提供器
 * 用于在路由守卫中刷新路由树
 */
export const RouterRefreshProvider = ({ children }: { children: React.ReactNode }) => {
	const [key, setKey] = useState(0);

	const refreshRoutes = () => {
		setKey(prev => prev + 1); // 修改 key 触发路由树重新挂载
	};

	return (
		<RouterRefreshContext.Provider value={{ key, refreshRoutes }}>
			{children}
		</RouterRefreshContext.Provider>
	);
};